//
//  MSLoginViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/1.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSLoginViewController.h"
#import "MSLoginTableViewController.h"
#import "MSRegistViewController.h"
#import "MSLeadViewController.h"

@interface MSLoginViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) GXButton *loginBtn;
@property (nonatomic, strong) GXButton *forgetBtn;
@property (nonatomic, strong) UITextField   *phoneTextField;
@property (nonatomic, strong) UITextField   *passwordTextField;
@property (nonatomic, strong) MSLoginTableViewController *tableVC;
@end

@implementation MSLoginViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"登录";
}

- (void)initUI{
    self.tableVC = [[MSLoginTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight);
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.scrollEnabled = NO;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 25)];
    headerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableHeaderView = headerView;
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    footerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableFooterView = footerView;
    
    self.loginBtn = [[GXButton alloc] initWithFrame:CGRectMake(10, 25, SCREEN_WIDTH-20, 45)];
    self.loginBtn.titleLabel.font = HeitiFont(14);
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.loginBtn.backgroundColor = kBlueColor;
    self.loginBtn.layer.cornerRadius = 2;
    [self.loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:self.loginBtn];
    
    self.forgetBtn = [[GXButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-95)/2, _loginBtn.bottom+15, 100, 40)];
    self.forgetBtn.titleLabel.font = HeitiFont(13);
    [self.forgetBtn setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [self.forgetBtn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
    [self.forgetBtn addTarget:self action:@selector(forgetClick) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:self.forgetBtn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [self.contentView addGestureRecognizer:tap];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateViewController:) name:MSViewControllerUpdateNotification object:nil];
}

- (void)testDataFunc{
    self.tableVC.dataArray = [NSMutableArray arrayWithArray:
                              @[[DOKeyValue key:@"mobile" value:@""placeHolder:@"请填写您的手机号码"],
                                [DOKeyValue key:@"password" value:@""placeHolder:@"请输入您的密码"]
                                ]
                             ];
    
    MSEditCell *cell1 = (MSEditCell *)[self.tableVC.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    MSEditCell *cell2 = (MSEditCell *)[self.tableVC.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]];
    
    self.phoneTextField = cell1.inputField;
    [self.phoneTextField addTarget:self action:@selector(customTextFieldEditingLoginChanged:) forControlEvents:UIControlEventEditingChanged];
    
    self.passwordTextField = cell2.inputField;
    self.passwordTextField.keyboardType = UIKeyboardTypeDefault;
    self.phoneTextField.delegate = (id)self;
    self.passwordTextField.delegate = (id)self;

}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"Login"]!=nil){
            [rootNavigation popToRootViewControllerAnimated:NO];
        }
    }
}

- (void)loginClick{
    if ([[self.phoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        [HDUtil showMessage:@"请输入手机号码"];
        return;
    }
    if (self.phoneTextField.text.length && ![HDUtil checkMoblieNumber:self.phoneTextField.text]) {
        [HDUtil showMessage:@"请输入正确的手机号码"];
        return;
    }
    if ([[self.passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        [HDUtil showMessage:@"请输入密码"];
        return;
    }
    NSDictionary *params = @{@"userName":self.phoneTextField.text,
                             @"password":self.passwordTextField.text};
    [netEngine requestLoginWithTarget:self.VctrID hudView:self.contentView params:params];
//    [rootNavigation popToRootViewControllerAnimated:NO];
//    
//    NSUserDefaults *defaultes = [NSUserDefaults standardUserDefaults];
//    [defaultes setObject:@"y" forKey:LoginState];
//    [defaultes synchronize];
}

- (void)forgetClick{
    MSRegistViewController *vc = [[MSRegistViewController alloc] init];
    vc.registType = MSRegistType_ForgetPassword;
    [rootNavigation pushViewController:vc animated:YES];
}

- (void)tapClick{
    [self.view endEditing:YES];
}

#pragma mark - UITextFieldEditingChanged
- (void)customTextFieldEditingLoginChanged:(UITextField *)textField{
    NSInteger phoneNumLength = (textField.text.length < kPhoneNumberLength) ? textField.text.length : kPhoneNumberLength;
    textField.text = [textField.text substringWithRange:NSMakeRange(0, phoneNumLength)];
}

@end
